package com.mengchengnet.chaperonage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.domain.base.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/10/23.
 */

public class ServiceAdapter extends BaseAdapter{
    private LayoutInflater mInflater = null;
    private List<Service> list;
    private Context mContext;

    public ServiceAdapter(Context context, List<Service> list){
        this.mInflater = LayoutInflater.from(context);
        this.mContext=context;
        this.list=list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //如果缓存convertView为空，则需要创建View
        if(convertView == null)
        {
            holder = new ViewHolder();
            //根据自定义的Item布局加载布局
            convertView = mInflater.inflate(R.layout.item_service, null);
            holder.name= (TextView) convertView.findViewById(R.id.item_service_tv_type);
            holder.time= (TextView) convertView.findViewById(R.id.item_service_tv_time);
            holder.price=(TextView) convertView.findViewById(R.id.item_service_tv_price);

            //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.name.setText(list.get(position).getName());
        holder.time.setText(list.get(position).getTime());
        holder.price.setText(list.get(position).getPrice());

        return convertView;
    }

    //ViewHolder静态类
    static class ViewHolder
    {
        public TextView name;
        public TextView price;
        public TextView time;
    }
}
